<template>
    <DrawerPro
        v-model="open"
        :header="$t('commons.button.' + operateReq.operate)"
        :resource="resourceName"
        @close="handleClose"
    >
        <div v-loading="loading">
            <el-descriptions direction="vertical">
                <el-descriptions-item>
                    <el-link @click="toLink(app.website)">
                        <el-icon><OfficeBuilding /></el-icon>
                        <span>{{ $t('app.appOfficeWebsite') }}</span>
                    </el-link>
                </el-descriptions-item>
                <el-descriptions-item>
                    <el-link @click="toLink(app.document)">
                        <el-icon><Document /></el-icon>
                        <span>{{ $t('app.document') }}</span>
                    </el-link>
                </el-descriptions-item>
                <el-descriptions-item>
                    <el-link @click="toLink(app.github)">
                        <el-icon><Link /></el-icon>
                        <span>{{ $t('app.github') }}</span>
                    </el-link>
                </el-descriptions-item>
            </el-descriptions>
            <el-form @submit.prevent ref="updateRef" :rules="rules" label-position="top" :model="operateReq">
                <el-form-item :label="$t('app.versionSelect')" prop="detailId">
                    <el-select v-model="operateReq.version" @change="getVersions(operateReq.version)">
                        <el-option
                            v-if="operateReq.operate == 'ignore'"
                            :value="'all'"
                            :label="$t('commons.table.all') + $t('app.version')"
                        ></el-option>
                        <el-option
                            v-for="(version, index) in versions"
                            :key="index"
                            :value="version.version"
                            :label="version.version"
                        ></el-option>
                    </el-select>
                </el-form-item>
                <el-form-item prop="backup" v-if="operateReq.operate === 'upgrade'">
                    <el-checkbox v-model="operateReq.backup" :label="$t('app.backupApp')" />
                    <span class="input-help">
                        <el-text type="warning">{{ $t('app.backupAppHelper') }}</el-text>
                    </span>
                </el-form-item>
                <el-form-item prop="pullImage" v-if="operateReq.operate === 'upgrade'">
                    <el-checkbox v-model="operateReq.pullImage" :label="$t('app.pullImage')" size="large" />
                    <span class="input-help">{{ $t('app.pullImageHelper') }}</span>
                </el-form-item>
            </el-form>
            <div v-if="operateReq.operate === 'upgrade'">
                <el-text type="danger" v-if="isEdit">{{ $t('app.isEdirWarn') }}</el-text>
                <el-text type="warning" v-else>{{ $t('app.upgradeWarn') }}</el-text>
                <div>
                    <el-button @click="openDiff()" :type="isEdit ? 'warning' : 'primary'">
                        {{ $t('app.showDiff') }}
                    </el-button>
                </div>
                <el-checkbox v-model="useNewCompose" :label="$t('app.useCustom')" size="large" />
                <div v-if="useNewCompose">
                    <el-text type="danger" v-if="!isEdit">{{ $t('app.useCustomHelper') }}</el-text>
                    <CodemirrorPro v-model="newCompose" mode="yaml"></CodemirrorPro>
                </div>
            </div>
        </div>
        <template #footer>
            <el-button @click="handleClose" :disabled="loading">{{ $t('commons.button.cancel') }}</el-button>
            <el-button type="primary" @click="onOperate" :disabled="versions == null || loading">
                {{ $t('commons.button.confirm') }}
            </el-button>
        </template>
        <Diff ref="composeDiffRef" @confirm="getNewCompose" />
    </DrawerPro>
    <TaskLog ref="taskLogRef" />
</template>
<script lang="ts" setup>
import Diff from './diff/index.vue';
import TaskLog from '@/components/log/task/index.vue';
import CodemirrorPro from '@/components/codemirror-pro/index.vue';

import { App } from '@/api/interface/app';
import { getAppUpdateVersions, ignoreUpgrade, installedOp } from '@/api/modules/app';
import { getAppStoreConfig } from '@/api/modules/setting';
import i18n from '@/lang';
import { ElMessageBox, FormInstance } from 'element-plus';
import { reactive, ref, onBeforeUnmount } from 'vue';
import { MsgSuccess } from '@/utils/message';
import { Rules } from '@/global/form-rules';
import bus from '@/global/bus';
import { v4 as uuidv4 } from 'uuid';

const composeDiffRef = ref();
const updateRef = ref<FormInstance>();
const open = ref(false);
const loading = ref(false);
const versions = ref<App.VersionDetail[]>();
const operateReq = reactive({
    detailId: 0,
    operate: 'upgrade',
    installId: 0,
    backup: true,
    pullImage: true,
    version: '',
    dockerCompose: '',
    taskID: '',
});
const resourceName = ref('');
const rules = ref<any>({
    detailId: [Rules.requiredSelect],
});
const app = ref();
const oldContent = ref('');
const newContent = ref('');
const em = defineEmits(['close']);
const handleClose = () => {
    open.value = false;
    em('close', open);
};

const newCompose = ref('');
const useNewCompose = ref(false);
const appInstallID = ref(0);
const taskLogRef = ref();
const ignoreAppReq = reactive({
    appID: 0,
    appDetailID: 0,
    scope: 'app',
});
const isEdit = ref(false);

const toLink = (link: string) => {
    window.open(link, '_blank');
};

const openDiff = async () => {
    if (newContent.value === '') {
        await getVersions(operateReq.version);
    }
    composeDiffRef.value.acceptParams(oldContent.value, newContent.value);
};

const getNewCompose = (compose: string) => {
    if (compose !== '') {
        newCompose.value = compose;
        useNewCompose.value = true;
    } else {
        newCompose.value = newContent.value;
        useNewCompose.value = false;
    }
};

const initData = async () => {
    const config = await getAppStoreConfig();
    newCompose.value = '';
    useNewCompose.value = false;
    operateReq.backup = config.data.upgradeBackup == 'Enable';
    operateReq.pullImage = true;
    operateReq.dockerCompose = '';
};

const acceptParams = (appInstall: App.AppInstallDto, op: string) => {
    initData();
    isEdit.value = appInstall.isEdit;
    operateReq.installId = appInstall.id;
    operateReq.operate = op;
    resourceName.value = appInstall.name;
    app.value = appInstall.app;
    oldContent.value = appInstall.dockerCompose;
    appInstallID.value = appInstall.id;
    getVersions('');
    ignoreAppReq.appID = appInstall.app.id;
    if (op === 'ignore') {
        operateReq.detailId = -1;
    }
    open.value = true;
};

const getVersions = async (version: string) => {
    const req = {
        appInstallID: appInstallID.value,
    };
    if (version !== '') {
        req['updateVersion'] = version;
    }
    try {
        const res = await getAppUpdateVersions(req);
        versions.value = res.data || [];
        if (res.data != null && res.data.length > 0) {
            let item = res.data[0];
            if (version != '') {
                item = res.data.find((v) => v.version === version);
            }
            operateReq.detailId = item.detailId;
            operateReq.version = item.version;
            newContent.value = item.dockerCompose;
            newCompose.value = item.dockerCompose;
            useNewCompose.value = false;
        }
    } catch (error) {}
};

const openTaskLog = (taskID: string) => {
    taskLogRef.value.openWithTaskID(taskID);
};

const operate = async () => {
    loading.value = true;
    if (operateReq.operate === 'upgrade') {
        if (useNewCompose.value) {
            operateReq.dockerCompose = newCompose.value;
        }
        const taskID = uuidv4();
        operateReq.taskID = taskID;
        await installedOp(operateReq)
            .then(() => {
                bus.emit('upgrade', true);
                handleClose();
                openTaskLog(taskID);
            })
            .finally(() => {
                loading.value = false;
            });
    } else {
        await ignoreUpgrade(ignoreAppReq)
            .then(() => {
                MsgSuccess(i18n.global.t('commons.msg.operationSuccess'));
                bus.emit('upgrade', true);
                handleClose();
            })
            .finally(() => {
                loading.value = false;
            });
    }
};

const onOperate = async () => {
    ElMessageBox.confirm(
        i18n.global.t('app.operatorHelper', [i18n.global.t('commons.button.' + operateReq.operate)]),
        i18n.global.t('commons.button.upgrade'),
        {
            confirmButtonText: i18n.global.t('commons.button.confirm'),
            cancelButtonText: i18n.global.t('commons.button.cancel'),
            type: 'info',
        },
    ).then(() => {
        operate();
    });
};

onBeforeUnmount(() => {
    bus.off('upgrade');
});

defineExpose({
    acceptParams,
});
</script>
